#include "logger.hpp"
#include <iostream>
#include "util.hpp"
#include "db.hpp"
#include "room.hpp"
#include "session.hpp"
#include "matcher.hpp"
#include "server.hpp"
#define HOST "127.0.0.1"
#define USER "root"
#define PASSWD "sa1212@@"
#define DBNAME "online_gobang"
#define PORT 3306
void Log_test(){
    LOG("%s","hello world");
    INF_LOG("hello");
}

void json_util_test(){
    //序列化
    Json::Value root;
    root["name"]="张三";
    root["age"]=18;
    root["score"]=700;
    std::string str;
    bool ret=json_util::serialize(root,str);
    if(ret){
        std::cout<<str<<std::endl;
    }
    //反序列化
    Json::Value stu;
    bool ret2=json_util::unserialize(str,stu);
    if(ret2){
        printf("%s %u %f\n",stu["name"].asCString(),stu["age"].asUInt(),stu["score"].asFloat());
    }
}

void mysql_util_test(){
    MYSQL*mysql=mysql_util::mysql_create(HOST,USER,PASSWD,DBNAME,PORT);
    if(mysql==nullptr){
        ERR_LOG("mysql create fail");
        return;
    }
    std::string str="insert into stu value(null,12,'王五')";
    mysql_util::mysql_exec(mysql,str);
}


void string_util_test(){
    std::string str="SSID=1";
    std::vector<std::string> v;
    std::string seq="=";
    string_util::split(str,seq,v);
    for(auto e:v){
        std::cout<<e<<std::endl;
    }
}


void file_util_test(){
    std::string str;
    const std::string filename="makefile";
    if(file_util::read(filename,str)){
        std::cout<<str<<std::endl;
    }

}

void user_table_test(){
    user_table ut(HOST,USER,PASSWD,DBNAME,PORT);
    Json::Value user;
    user["username"]="zhangsan";
    user["password"]="12345";
    // ut.insert(user);

    // bool ret=ut.login(user);
    bool ret=ut.select_by_name("zhangsan",user);
    if(ret==false)return;
    ut.lose(user["id"].asInt64());
    ret=ut.select_by_id(user["id"].asInt64(),user);
    if(ret==false) return;
    printf("%10s%10s%10s%12s%10s\n","id","username","score","total_count","win_count");
    printf("%10ld%10s%10ld%10ld%10ld\n",user["id"].asInt64(),
            user["username"].asCString(),
            //user["password"].asCString(),
            user["score"].asInt64(),
            user["total_count"].asInt64(),
            user["win_count"].asInt64()); 
}

int main()
{
    // user_table_test();
    gobang_server gs(HOST,USER,PASSWD,DBNAME,PORT);
    gs.start(8080);
    // string_util_test();
    return 0;
}